package com.ailink.design.atomic;

import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAdder;

/**
 * Created by Administrator on 2018/4/28
 */
public class LongAdderTest {

    public static void main(String[] args) {
        System.out.println(Runtime.getRuntime().availableProcessors());
        LongAdderTest longAdderTest = new LongAdderTest();
        ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(100, 100, 60, TimeUnit.SECONDS, new LinkedBlockingDeque<>());
        for (int i = 0; i < 100000; i++) {
            threadPoolExecutor.execute(() -> longAdderTest.add());
        }
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(longAdderTest.longAdder.sum());
            System.out.println(longAdderTest.longAdder.toString());
        }
    }

    private LongAdder longAdder = new LongAdder();

    private AtomicInteger atomicInteger = new AtomicInteger();
    private AtomicLong atomicLong = new AtomicLong();

    public void add() {
        longAdder.increment();
        atomicInteger.incrementAndGet();
        atomicLong.incrementAndGet();
    }
}
